define(['jquery', 'bootstrap', 'backend', 'table', 'form','csmwj_xcore','csmwj_csminputstyle','jquery.simple-color'], function ($, undefined, Backend, Table, Form,ccore,csminputstyle,undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'csmwj/papertheme/index' + location.search,
                    add_url: 'csmwj/papertheme/add',
                    edit_url: 'csmwj/papertheme/edit',
                    del_url: 'csmwj/papertheme/del',
                    multi_url: 'csmwj/papertheme/multi',
                    import_url: 'csmwj/papertheme/import',
                    table: 'csmwj_paper',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'locales', title: __('Locales'), searchList: {"zh":__('Locales zh'),"en":__('Locales en')}, formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'admin_id', title: __('Admin_id')},
                        {field: 'cfghasdatelimit', title: __('Cfghasdatelimit'), searchList: {"Y":__('Cfghasdatelimit y'),"N":__('Cfghasdatelimit n')}, formatter: Table.api.formatter.normal},
                        {field: 'cfgbegintime', title: __('Cfgbegintime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'cfgendtime', title: __('Cfgendtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'cfgmaxanswercount', title: __('Cfgmaxanswercount')},
                        {field: 'cfgresultmsg', title: __('Cfgresultmsg'), operate: 'LIKE'},
                        {field: 'cfgresultmsgimage', title: __('Cfgresultmsgimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'cfghastimelimit', title: __('Cfghastimelimit'), searchList: {"Y":__('Cfghastimelimit y'),"N":__('Cfghastimelimit n')}, formatter: Table.api.formatter.normal},
                        {field: 'cfgtimelimitsecs', title: __('Cfgtimelimitsecs')},
                        {field: 'cfgdisplayscore', title: __('Cfgdisplayscore'), searchList: {"Y":__('Cfgdisplayscore y'),"N":__('Cfgdisplayscore n')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden'),"draft":__('Draft')}, formatter: Table.api.formatter.status},
                        {field: 'themebannertype', title: __('Themebannertype'), searchList: {"EMPTY":__('Themebannertype empty'),"STRING":__('Themebannertype string'),"IMAGE":__('Themebannertype image'),"IMAGES":__('Themebannertype images')}, formatter: Table.api.formatter.normal},
                        {field: 'themebannerstringstyle', title: __('Themebannerstringstyle'), operate: 'LIKE'},
                        {field: 'themebannerstringdivstyle', title: __('Themebannerstringdivstyle'), operate: 'LIKE'},
                        {field: 'themebannerimage', title: __('Themebannerimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'themebannerimages', title: __('Themebannerimages'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'themelogoimage', title: __('Themelogoimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'themelogodivstyle', title: __('Themelogodivstyle'), operate: 'LIKE'},
                        {field: 'themetitlefont', title: __('Themetitlefont'), operate: 'LIKE'},
                        {field: 'themetitledivstyle', title: __('Themetitledivstyle'), operate: 'LIKE'},
                        {field: 'themequestionfont', title: __('Themequestionfont'), operate: 'LIKE'},
                        {field: 'themequestiondivstyle', title: __('Themequestiondivstyle'), operate: 'LIKE'},
                        {field: 'themeoptionsfont', title: __('Themeoptionsfont'), operate: 'LIKE'},
                        {field: 'themeoptionsdivstyle', title: __('Themeoptionsdivstyle'), operate: 'LIKE'},
                        {field: 'themesubmitstring', title: __('Themesubmitstring'), operate: 'LIKE'},
                        {field: 'themesubmitdivstyle', title: __('Themesubmitdivstyle'), operate: 'LIKE'},
                        {field: 'themewelcomefont', title: __('Themewelcomefont'), operate: 'LIKE'},
                        {field: 'themewelcomedivstyle', title: __('Themewelcomedivstyle'), operate: 'LIKE'},
                        {field: 'themeendfont', title: __('Themeendfont'), operate: 'LIKE'},
                        {field: 'themeendedivstyle', title: __('Themeendedivstyle'), operate: 'LIKE'},
                        {field: 'themetitleshow', title: __('Themetitleshow'), searchList: {"Y":__('Themetitleshow y'),"N":__('Themetitleshow n')}, formatter: Table.api.formatter.normal},
                        {field: 'themequestionnoshow', title: __('Themequestionnoshow'), searchList: {"Y":__('Themequestionnoshow y'),"N":__('Themequestionnoshow n')}, formatter: Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            let that = this;
            let id = $("#c-id").val();
            let locales = $("#c-locales").val();
            let url = Config.rooturl+Fast.api.fixurl("/assets/addons/csmwj/h5/#/pages/csmwj/paperpreview?id="+id);
            //alert(url);
            $("#demo-modal").attr("src",url);
            //document.getElementById('demo-modal').contentWindow.location = ;
            $('#c-themetopnavigationtitle').change(function(){that._renderPreview();});
            $('#c-themebannertype').change(function(){that._renderPreview();});
            $('#c-themebannerstring').change(function(){that._renderPreview();});
            $('#c-themebannerstringstyle').change(function(){that._renderPreview();});
            $('#c-themebannerstringdivstyle').change(function(){that._renderPreview();});
            $('#c-themebannerimage').change(function(){that._renderPreview();});
            $('#c-themebannerimages').change(function(){that._renderPreview();});
            $('#c-themelogoimage').change(function(){that._renderPreview();});
            $('#c-themelogodivstyle').change(function(){that._renderPreview();});
            $('#c-themetitlefont').change(function(){that._renderPreview();});
            $('#c-themetitledivstyle').change(function(){that._renderPreview();});
            $('#c-themequestionfont').change(function(){that._renderPreview();});
            $('#c-themequestiondivstyle').change(function(){that._renderPreview();});
            $('#c-themeoptionsfontcolor').change(function(){that._renderPreview();});
            $('#c-themeoptionsselectedcolor').change(function(){that._renderPreview();});
            $('#c-themesubmitstring').change(function(){that._renderPreview();});
            $('#c-themesubmitdivstyle').change(function(){that._renderPreview();});
            $('#c-themewelcomefont').change(function(){that._renderPreview();});
            $('#c-themewelcomedivstyle').change(function(){that._renderPreview();});
            $('#c-themeendfont').change(function(){that._renderPreview();});
            $('#c-themeendedivstyle').change(function(){that._renderPreview();});
            $('#c-themetitleshow').change(function(){that._renderPreview();});
            $('#c-themequestionnoshow').change(function(){that._renderPreview();});
 
            $('#s-themebannerstringstyle').csminputstyle($("#c-themebannerstringstyle").val(),function(styleString){
                $('#c-themebannerstringstyle').val(styleString);
                that._renderPreview();
            },1);
            $('#s-themebannerstringdivstyle').csminputstyle($("#c-themebannerstringdivstyle").val(),function(styleString){
                $('#c-themebannerstringdivstyle').val(styleString);
                that._renderPreview();
            },2);
            $('#s-themelogodivstyle').csminputstyle($("#c-themelogodivstyle").val(),function(styleString){
                $('#c-themelogodivstyle').val(styleString);
                that._renderPreview();
            },2);

            $('#s-themetitlefont').csminputstyle($("#c-themetitlefont").val(),function(styleString){
                $('#c-themetitlefont').val(styleString);
                that._renderPreview();
            },1);
            $('#s-themetitledivstyle').csminputstyle($("#c-themetitledivstyle").val(),function(styleString){
                $('#c-themetitledivstyle').val(styleString);
                that._renderPreview();
            },2);
            $('#s-themequestionfont').csminputstyle($("#c-themequestionfont").val(),function(styleString){
                $('#c-themequestionfont').val(styleString);
                that._renderPreview();
            },1);
            $('#s-themequestiondivstyle').csminputstyle($("#c-themequestiondivstyle").val(),function(styleString){
                $('#c-themequestiondivstyle').val(styleString);
                that._renderPreview();
            },2);
            $('#s-themeoptionsfontcolor').csminputstyle($("#c-themeoptionsfontcolor").val(),function(styleString){
                $('#c-themeoptionsfontcolor').val(styleString);
                that._renderPreview();
            },4);
            $('#s-themeoptionsselectedcolor').csminputstyle($("#c-themeoptionsselectedcolor").val(),function(styleString){
                $('#c-themeoptionsselectedcolor').val(styleString);
                that._renderPreview();
            },4);       

            $('#s-themesubmitdivstyle').csminputstyle($("#c-themesubmitdivstyle").val(),function(styleString){
                $('#c-themesubmitdivstyle').val(styleString);
                that._renderPreview();
            },3);   
            $('#s-themewelcomefont').csminputstyle($("#c-themewelcomefont").val(),function(styleString){
                $('#c-themewelcomefont').val(styleString);
                that._renderPreview();
            },1);   
            $('#s-themewelcomedivstyle').csminputstyle($("#c-themewelcomedivstyle").val(),function(styleString){
                $('#c-themewelcomedivstyle').val(styleString);
                that._renderPreview();
            },2);   
            $('#s-themeendfont').csminputstyle($("#c-themeendfont").val(),function(styleString){
                $('#c-themeendfont').val(styleString);
                that._renderPreview();
            },1);  
            $('#s-themeendedivstyle').csminputstyle($("#c-themeendedivstyle").val(),function(styleString){
                $('#c-themeendedivstyle').val(styleString);
                that._renderPreview();
            },2); 

            // ccore.cTriggerselectById("c-themebannertype");
        },
        _renderPreview:function(){
            let that = this;
            let param = {
                "themetopnavigationtitle": $('#c-themetopnavigationtitle').val(),
                "themebannertype": $('#c-themebannertype').val(),
                "themebannerstring": $('#c-themebannerstring').val(),
                "themebannerstringstyle": $('#c-themebannerstringstyle').val(),
                "themebannerstringdivstyle": $('#c-themebannerstringdivstyle').val(),
                "themelogoimage": that._cdnurl($('#c-themelogoimage').val()),
                "themebannerimage": that._cdnurl($('#c-themebannerimage').val()),
                "themebannerimages": that._cdnurls($('#c-themebannerimages').val()),
                "themelogodivstyle": $('#c-themelogodivstyle').val(),
                "themetitlefont": $('#c-themetitlefont').val(),
                "themetitledivstyle": $('#c-themetitledivstyle').val(),
                "themequestionfont": $('#c-themequestionfont').val(),
                "themequestiondivstyle": $('#c-themequestiondivstyle').val(),
                "themeoptionsfontcolor": $('#c-themeoptionsfontcolor').val(),
                "themeoptionsselectedcolor": $('#c-themeoptionsselectedcolor').val(),
                "themesubmitstring": $('#c-themesubmitstring').val(),
                "themesubmitdivstyle": $('#c-themesubmitdivstyle').val(),
                "themewelcomefont": $('#c-themewelcomefont').val(),
                "themewelcomedivstyle": $('#c-themewelcomedivstyle').val(),
                "themeendfont": $('#c-themeendfont').val(),
                "themeendedivstyle": $('#c-themeendedivstyle').val(),
                "themetitleshow": $('#c-themetitleshow').val(), 
                "themequestionnoshow": $('#c-themequestionnoshow').val(),	
            };
            console.log("admin call themepreview:",param);
            setTimeout(function(){
                document.getElementById('demo-modal').contentWindow.themepreview(param);//调用uview代码中的Js方法，uview中将themepreview暴露到body中
            },500);
            
        },
        _cdnurl:function(url){
            let sr = null;
            if(url!=null && url!=''){
                sr = Config.rooturl+url;
            }
            return sr;
        },
        _cdnurls:function(urls){
            let sr = [];
            if(urls!=null && urls!=''){
                let urlarr = urls.split(',');
                for(let index in urlarr){
                    sr.push(Config.rooturl+Fast.api.cdnurl(urlarr[index]));
                }
            }
            return sr.join(',');
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
